
import 'package:intl/intl.dart';

class DateUtil {

  static String formatSimpleDate(DateTime date) {
    return DateFormat("yyyyMMddHHmmssSSS").format(date);
  }

  static String? formatDate(DateTime? date) {
    if (date == null) return null;
    return DateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
  }

  static String formatDate2(DateTime date, String format) {
    return DateFormat(format).format(date);
  }

  static bool hasExceededTime(DateTime date, int minute) {
    return date.add(Duration(minutes: minute)).isBefore(DateTime.now());
  }

  static bool isSameDay(DateTime date1, DateTime date2) {
    return date1.year == date2.year && date1.month == date2.month && date1.day == date2.day;
  }

  static bool isSameMonth(DateTime date1, DateTime date2) {
    return date1.year == date2.year && date1.month == date2.month;
  }

  static bool isSameYear(DateTime date1, DateTime date2) {
    return date1.year == date2.year;
  }

  static String? sessionShowTime(DateTime? date) {
    if (date == null) return null;
    DateTime now = DateTime.now();
    if (isSameYear(date, now)) {
      if (isSameDay(date!, now)) {
        return DateUtil.formatDate2(date, "HH:mm");
      }
      var week = now.weekday;

      now = now.add(Duration(days: -1));
      if (isSameDay(date!, now)) {
        return "昨天";
      }
      for (var w=week-1; w>1; w--) {
        now = now.add(Duration(days: -1));
        if (isSameDay(date!, now)) {
          return getWeek(date!);
        }
      }
      return DateUtil.formatDate2(date!, "MM月dd日");
    } else {
      return DateUtil.formatDate2(date, "yyyy年MM月dd日");
    }
  }

  static int nowTime() {
    return DateTime.now().millisecond;
  }

  static const weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  static String getWeek(DateTime date) {
    var week = date.weekday;
    return weeks[week-1];
  }
}
